﻿using System;
using System.IO;
using visinia.SmartEngine;

namespace visinia.web.admin.pages
{
    public partial class BulkUploadImages : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
                Populate(Server.MapPath(WebRoots.GetResourcesRoot));
        }

        protected void UPLOAD_Click(object sender, EventArgs e)
        {
            string destination = Directories.SelectedValue;
            AddImages(destination);
        }

        private void AddImages(string folder)
        {
            string path = Server.MapPath(WebRoots.GetResourcesRoot);
            if (folder != "\\")
                path += folder + "\\";
            if (FileUpload1.HasFile)
                FileUpload1.SaveAs(ResolveDuplicate(path, FileUpload1.FileName));

            if (FileUpload2.HasFile)
                FileUpload2.SaveAs(ResolveDuplicate(path, FileUpload2.FileName));

            if (FileUpload3.HasFile)
                FileUpload3.SaveAs(ResolveDuplicate(path, FileUpload3.FileName));

            if (FileUpload4.HasFile)
                FileUpload4.SaveAs(ResolveDuplicate(path, FileUpload4.FileName));

            if (FileUpload5.HasFile)
                FileUpload5.SaveAs(ResolveDuplicate(path, FileUpload5.FileName));

            if (FileUpload6.HasFile)
                FileUpload6.SaveAs(ResolveDuplicate(path, FileUpload6.FileName));

            if (FileUpload7.HasFile)
                FileUpload7.SaveAs(ResolveDuplicate(path, FileUpload7.FileName));

            if (FileUpload8.HasFile)
                FileUpload8.SaveAs(ResolveDuplicate(path, FileUpload8.FileName));
        }

        private string ResolveDuplicate(string folder, string filename)
        {
            string path = folder + filename;
            if (System.IO.File.Exists(path))
            {
                int index = filename.LastIndexOf('.');
                string name = filename.Substring(0, index);
                string extension = filename.Substring(index);
                int counter = 1;
                while (System.IO.File.Exists(path))
                {
                    path = folder + name + counter.ToString() + extension;
                    counter++;
                }
            }
            return path;
        }

        protected void CANCEL_Click(object sender, EventArgs e)
        {
            Response.Redirect(Request.RawUrl);
        }

        private void Populate(string path)
        {
            DirectoryInfo dir = new DirectoryInfo(path);
            if (dir.Exists)
            {
                Directories.Items.Add("\\");
                GetDirectories(dir.GetDirectories(), dir.FullName);
            }
        }

        private void GetDirectories(DirectoryInfo[] subDirs, string path)
        {
            foreach (DirectoryInfo subDir in subDirs)
            {
                if ((subDir.Attributes & FileAttributes.Hidden) == FileAttributes.Hidden)
                    continue;
                DirectoryInfo[] subSubDirs;
                Directories.Items.Add(subDir.FullName.Substring(path.Length));
                subSubDirs = subDir.GetDirectories();
                if (subSubDirs.Length != 0)
                {
                    GetDirectories(subSubDirs, path);
                }
            }
        }
    }
}
